/*
 * @Copyright: 2025 Copyright (C) AMCAX, All rights reserved.
 * @Author: Zhang
 * @Date: 2025/7/24 19:39
 * @LastEditTime: 2025/7/24 19:39
 * @LastEditors: Zhang
 * @Description:
 */
import { TopoDialogProps, NMMeshBaseCommand } from '../type';

import { CmdDialogForm } from 'nextcae-base';
import { useEffect, useState } from 'react';
import './index.scss';
import { schema, uiSchema } from './data';
import { widgetManager } from '@/common/managers';
import { surfProcess } from '@/domain';
import { makeId } from '@/common/utils';

const SurfaceMergeDialog = ({ title, onClose }: TopoDialogProps) => {
  const [formData, setFormData] = useState<any>({
    Selection: { source: [] },
  });

  useEffect(() => {
    widgetManager.hint('进入曲面拼接命令!');
    return () => {
      widgetManager.hint();
    };
  }, []);

  const onSubmit = async () => {
    const { Selection } = formData;
    if (Selection?.source?.length !== 0) {
      const patchesData: string[] = Selection?.source.map((item) =>
        makeId(item),
      );
      const params = {
        surface: patchesData,
      };
      await surfProcess.montageSurf(params);
      onClose();
    }
  };

  return (
    <>
      <CmdDialogForm
        title={title}
        titleIcon={'commands/surfaceFeatures/montage-surface'}
        schema={schema}
        extraUiSchema={uiSchema}
        formData={formData}
        fields={{}}
        onChange={(data) => {
          setFormData(data.formData);
        }}
        onSubmit={onSubmit}
        onClose={onClose}
        isFormDataValid={formData?.Selection?.source?.length > 0}
        width='300px'
        formClassName='structure-form'
      />
    </>
  );
};
export const cmdSurfaceMontage: NMMeshBaseCommand = {
  id: 'cmdSurfaceMontage',
  icon: 'commands/surfaceFeatures/montage-surface',
  label: '曲面拼接',
  create: (label: string, onClose: () => void, props) => (
    <SurfaceMergeDialog
      title={label}
      onClose={onClose}
      props={props}
      placement='topLeft'
    />
  ),
};
